package test;

import java.io.*;
import java.nio.ByteBuffer;

/**
 * Created by Administrator on 2017/1/9 0009.
 */
public class EnCode {
    public static void main(String[] args) throws IOException {
        //String string = "\uE522 "; //
        String string = "Davina \uD83D\uDC20";
        string = test(string);


    }

    /*public void youxiao() throws IOException{
        byte[] bytes = "x".getBytes("utf-8");
        ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
        int i = 0;
        while (i < bytes.length) {
            short b = bytes[i];
            if (b > 0) {
                buffer.put(bytes[i++]);
                continue;
            }
            b += 256;
            if ((b ^ 0xC0) >> 4 == 0) {
                buffer.put(bytes, i, 2);
                i += 2;
            } else if ((b ^ 0xE0) >> 4 == 0) {
                buffer.put(bytes, i, 3);
                i += 3;
            } else if ((b ^ 0xF0) >> 4 == 0) {
                i += 4;
            }
            //添加处理如b的指为-48等情况出现的死循环
            else {
                buffer.put(bytes[i++]);
                continue;
            }
        }
        buffer.flip();
        String tt = new String(buffer.array(), "utf-8");
    }*/

    public static String test(String text) throws IOException {
        byte[] tr = text.getBytes("utf8");
        String ts = new String(tr, "utf-8");


        byte[] bytes = text.getBytes("utf-8");
        ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
        int i = 0;
        while (i < bytes.length) {
            short b = bytes[i];
            if (b > 0) {
                buffer.put(bytes[i++]);
                continue;
            }
            b += 256;
            if ((b ^ 0xC0) >> 4 == 0) {
                buffer.put(bytes, i, 2);
                i += 2;
            } else if ((b ^ 0xE0) >> 4 == 0) {
                buffer.put(bytes, i, 3);
                i += 3;
            } else if ((b ^ 0xF0) >> 4 == 0) {
                i += 4;
            }
            //添加处理如b的指为-48等情况出现的死循环
            else {
                buffer.put(bytes[i++]);
                continue;
            }
        }
        buffer.flip();
        return new String(buffer.array(), "utf-8");
    }

    static public String filterOffUtf8Mb4(String text) throws UnsupportedEncodingException {
        byte[] bytes = text.getBytes("utf-8");
        ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
        int i = 0;
        while (i < bytes.length) {
            short b = bytes[i];
            if (b > 0) {
                buffer.put(bytes[i++]);
                continue;
            }
            b += 256;
            if ((b ^ 0xC0) >> 4 == 0) {
                buffer.put(bytes, i, 2);
                i += 2;
            } else if ((b ^ 0xE0) >> 4 == 0) {
                buffer.put(bytes, i, 3);
                i += 3;
            } else if ((b ^ 0xF0) >> 4 == 0) {
                i += 4;
            }
        }
        buffer.flip();
        return new String(buffer.array(), "utf-8");
    }
}
